/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                           *
 *                          Copyright (c) 2010                               *
 *                  Alen Stojanov [ ioi.alen@gmail.com ]                     *
 *                Jacobs University Bremen, Bremen, Germany                  *
 *                                                                           *
 *                          All Rights Reserved.                             *
 *                                                                           *
 *    Permission to use, copy, modify and distribute this software and       *
 *    its documentation is hereby granted, provided that both the            *
 *    copyright notice and this permission notice appear in all copies of    *
 *    the software, derivative works or modified versions, and any           *
 *    portions thereof, and that both notices appear in supporting           *
 *    documentation.                                                         *
 *                                                                           *
 *    This code is distributed in the hope that it will be useful, but       *
 *    WITHOUT ANY WARRANTY; without even the implied warranty of             *
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS       *
 *    DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER          *
 *    RESULTING FROM THE USE OF THIS SOFTWARE.                               *
 *                                                                           *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef TSTHREAD_H
#define TSTHREAD_H

#include <QtGui/QWidget>
#include <QProgressBar>
#include <QLabel>
#include <QThread>
#include <cmath>

/**
 * @class TSThread
 * @author Alen Stojanov, October 2010
 *
 * @brief Run particular job in a thread with possibility to
 * show the progress in the TSView, using progress bar
 *
 *	This class is able to update the progress bar depending
 *	on the progress that has been acomplished by using the
 *	update method.
 */
class TSThread : public QThread
{
    Q_OBJECT

public:
    /**
     * @brief Define thread without using progress bar or text label
     */
    TSThread();
    /**
     * @brief Define a thread with particular progress bar and thread
     * @param progressBar	The Qt progress bar object that will be updated
     * @param lblProgress	The Qt label fascilitating the description
     */
    TSThread(QProgressBar* progressBar, QLabel* lblProgress);
    /**
     * @brief Update the progress bar with the particular progress value
     *
     * The formula calculating the value sent to the progress bar is:
     * (progress + partial_qint64) * 100 / complete_qint64.
     * The progress will be expressed in percent, so it always has the
     * range [0,100]
     *
     * @param progress	The progress value
     */
    void update(qint64	 progress);
    /**
     * @brief Update the progress bar with the particular progress value
     *
     * The formula calculating the value sent to the progress bar is:
     * round((progress + partial_double) * 100 / complete_double)
     * The progress will be expressed in percent, so it always has the
     * range [0,100]
     *
     * @param progress	The progress value
     */
    void update(double progress);

    /**
     * @brief 		Update the text of the description label
     * @param text	The new description in the label
     */
    void updateText(QString text);

    ~TSThread();

protected:
    /**
     * The value of the whole task in integer
     */
    qint64 			complete_qint64;
    /**
     * The partial progress value of the whole task in integer
     */
    qint64			partial_qint64;
    /**
     * The value of the whole task in double
     */
    double 			complete_double;
    /**
     * The partial progress value of the whole task in double
     */
    double 			partial_double;

    /**
     * @brief Set the progress bar
     */
    void setProgressBar(QProgressBar* progressBar);
    /**
     * @brief Set the description label
     */
    void setLabel(QLabel* lblProgress);

    /**
     * The default run method which will be overloaded
     */
    void run();

private:
    /**
     * The progress bar that will be updated
     */
    QProgressBar* 	progressBar;
    /*
     * The label that gives description of the progress
     */
    QLabel*			lblProgress;

signals:
	/**
	 * Signal the new value of the progress bar
	 */
	void setValue(int value);
	/**
	 * Signal the new text of the description label
	 */
	void setText(const QString &);
};

#endif // TSTHREAD_H
